<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>医院管理系统</title>
    <!-- 引入外部资源 -->
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>

    <!-- Tailwind 配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#0f2b48',    // 深蓝主色
                        secondary: '#4CAF50',  // 辅助绿色
                        accent: '#1b3b5c',     // 强调色
                        light: '#e9f1f7',      // 浅色背景
                        dark: '#0a1c2f',       // 深色背景
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                },
            }
        }
    </script>

    <!-- 自定义工具类 -->
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .sidebar-item {
                @apply flex items-center gap-3 px-4 py-2.5 rounded-lg transition-all duration-200;
            }
            .sidebar-item:hover {
                @apply bg-accent/80 translate-x-1;
            }
            .sidebar-item.active {
                @apply bg-accent font-medium;
            }
            .card {
                @apply bg-white rounded-xl shadow-sm border border-gray-100 p-5 transition-all duration-300 hover:shadow-md;
            }
            .card-header {
                @apply flex justify-between items-center mb-4;
            }
            .btn {
                @apply px-4 py-2 rounded-lg font-medium transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2;
            }
            .btn-primary {
                @apply bg-secondary text-white hover:bg-secondary/90 focus:ring-secondary/50;
            }
            .btn-outline {
                @apply border border-gray-300 text-gray-700 hover:bg-gray-50 focus:ring-gray-200;
            }
        }
    </style>
</head>
<body class="bg-light font-sans">
<!-- 侧边栏 -->
<div id="sidebar" class="fixed top-0 left-0 h-full w-64 bg-primary shadow-lg z-20 transition-all duration-300">
    <div class="p-5 border-b border-accent">
        <div class="flex items-center gap-3">
            <i class="fa fa-hospital-o text-secondary text-xl"></i>
            <h1 class="text-white font-semibold text-lg">医院急诊管理系统</h1>
        </div>
    </div>

    <nav class="p-4 space-y-1">
        <a href="#" class="sidebar-item text-white active" onclick="loadPage('registration')">
            <i class="fa fa-file-text w-5 text-center"></i>
            <span>急诊快速申请</span>
        </a>
        <a href="#" class="sidebar-item text-white" onclick="loadPage('appointmentDetails')">
            <i class="fa fa-ambulance w-5 text-center"></i>
            <span>急诊处理</span>
        </a>
        <a href="#" class="sidebar-item text-white" onclick="loadPage('records')">
            <i class="fa fa-edit w-5 text-center"></i>
            <span>急诊结果</span>
        </a>
        <a href="#" class="sidebar-item text-white" onclick="loadPage('listRegistrationByPatientId')">
            <i class="fa fa-list-alt w-5 text-center"></i>
            <span>挂号记录</span>
        </a>
        <a href="#" class="sidebar-item text-white" onclick="loadPage('resume')">
            <i class="fa fa-user w-5 text-center"></i>
            <span>我的信息</span>
        </a>
    </nav>

    <!-- 侧边栏底部 -->
    <div class="absolute bottom-0 left-0 right-0 p-4 border-t border-accent">
        <div th:if="${session.user != null}">
            <div class="sidebar-item text-white" onclick="logout()">
                <i class="fa fa-sign-out w-5 text-center"></i>
                <span>退出登录</span>
            </div>
        </div>
    </div>
</div>

<!-- 主内容区域 -->
<div id="mainContent" class="ml-64 min-h-screen flex flex-col transition-all duration-300">
    <!-- 顶部导航栏 -->
    <header class="bg-white shadow-sm py-3 px-6 sticky top-0 z-10">
        <div class="flex justify-between items-center">
            <div class="flex items-center gap-3">
                <button class="lg:hidden text-gray-600 focus:outline-none" id="sidebarToggle">
                    <i class="fa fa-bars text-xl"></i>
                </button>
                <h2 class="text-xl font-semibold text-gray-800" id="pageTitle">病历信息</h2>
            </div>

            <div class="flex items-center gap-4">
                <div class="relative">
                    <button type="button"
                            class="p-2 rounded-full hover:bg-gray-100 transition-colors focus:outline-none"
                            onclick="loadPage('appointmentDetails')">
                        <i class="fa fa-bell-o text-gray-600"></i>
                        <span th:if="${hasAppointment}" class="absolute top-1 right-1 w-2 h-2 bg-red-500 rounded-full"></span>
                    </button>
                </div>

                <div class="flex items-center gap-3">
                    <div class="w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center text-primary">
                        <i class="fa fa-user"></i>
                    </div>
                    <div class="hidden md:block" th:if="${session.user != null}">
                        <div class="text-sm font-medium text-gray-800" th:text="${session.user.name}">张三</div>
                        <div class="text-xs text-gray-500" th:text="${session.user.role}">普通用户</div>
                    </div>
                    <div class="hidden md:block" th:if="${session.user == null}">
                        <a th:href="@{/login}" class="text-sm font-medium text-gray-800 hover:text-primary">
                            未登录
                        </a>
                    </div>

                </div>

            </div>
        </div>
    </header>

    <!-- 内容区域 - 使用iframe加载内容 -->
    <main class="flex-1 p-6 flex flex-col">
        <div id="loadingIndicator" class="flex items-center justify-center h-full">
            <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-primary"></div>
        </div>
        <iframe id="mainFrame" src="registration" class="w-full flex-1 border-none"></iframe>
    </main>

    <!-- 页脚 -->
    <footer class="bg-white py-4 px-6 border-t border-gray-200 text-center text-sm text-gray-500">
        <p>© 2025 医院管理系统 | 技术支持：308宿舍科技</p>
    </footer>
</div>

<script>
    // 加载页面内容到iframe
    function loadPage(pageName) {
        // 更新侧边栏活动状态
        document.querySelectorAll('.sidebar-item').forEach(item => {
            item.classList.remove('active');
        });

        // 设置点击的菜单项为活动状态
        const clickedItem = event.currentTarget;
        clickedItem.classList.add('active');

        // 更新页面标题
        const pageTitle = clickedItem.querySelector('span').textContent;
        document.getElementById('pageTitle').textContent = pageTitle;

        // 显示加载指示器
        const loadingIndicator = document.getElementById('loadingIndicator');
        const mainFrame = document.getElementById('mainFrame');

        loadingIndicator.style.display = 'flex';
        mainFrame.style.display = 'none';

        // 模拟加载延迟
        setTimeout(() => {
            // 更新iframe内容
            mainFrame.src = `${pageName}`;
            // 隐藏加载指示器，显示iframe
            mainFrame.onload = () => {
                loadingIndicator.style.display = 'none';
                mainFrame.style.display = 'block';
            };
        }, 300);

        // 阻止默认行为
        event.preventDefault();
    }

    // 退出登录
    function logout() {
        if (confirm('确定要退出登录吗？')) {
            // 发送请求给服务器，清除 session
            fetch('/logout', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ action: 'logout' })
            }).then(() => {
                // 清除本地 session（如果有的话）
                sessionStorage.clear();
                localStorage.clear();

                // 跳转到登录页
                window.location.href = '/login';
            });
        }
    }


    // 页面加载完成后初始化
    document.addEventListener('DOMContentLoaded', function() {
        // 隐藏加载指示器
        document.getElementById('loadingIndicator').style.display = 'none';
        document.getElementById('mainFrame').style.display = 'block';

        // 侧边栏切换功能
        document.getElementById('sidebarToggle').addEventListener('click', function() {
            const sidebar = document.getElementById('sidebar');
            const mainContent = document.getElementById('mainContent');

            if (sidebar.classList.contains('w-64')) {
                sidebar.classList.remove('w-64');
                sidebar.classList.add('w-0');
                mainContent.classList.remove('ml-64');
                mainContent.classList.add('ml-0');
            } else {
                sidebar.classList.remove('w-0');
                sidebar.classList.add('w-64');
                mainContent.classList.remove('ml-0');
                mainContent.classList.add('ml-64');
            }
        });
    });
</script>
</body>
</html>
